package com.edoomath.api.web.controller.course;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.edoomath.api.servcice.UserCourseCustomizeService;
import com.edoomath.api.servcice.UserService;
import com.edoomath.entity.user.User;
import com.edoomath.entity.usercourse.UserCourseCustomize;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/user-course-customizes")
public class UserCourseCustomizeController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserCourseCustomizeController.class);
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserCourseCustomizeService userCourseCustomizeService;
	
	/**
	 * 
	 * @param unionid
	 * @param requestBody
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public HttpResult basicData() {
		
		HttpResult httpResult = new HttpResult();
		
        
        
        try {
        	// using project relative folder;
        	File jsonFile = ResourceUtils.getFile("classpath:course-customizes.json");
			logger.info("start create course customize with baisc data [{}]", jsonFile);
			
			Map<String, Object> jsonString = JsonUtils.parse(jsonFile);
			logger.info("start create course customize with baisc data [{}]", JsonUtils.toJson(jsonString));
			
			return httpResult.success().data(jsonString);
			
		} catch (IOException e) {
			logger.error("ERROR HERE:", e);
			return httpResult.fail("500").message("获取基础数据失败");
		}
	}
	
	/**
	 * 
	 * @param unionid
	 * @param userCourseCustomize
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult create(@RequestParam(value = "unionid") String unionid,
    						 @RequestBody String requestBody) {
		
		HttpResult httpResult = new HttpResult();
		
		try {
			
			logger.info("start create course customize with params unionid [{}] userCourseCustomize [{}]", unionid, requestBody);
			
			User user = userService.load(unionid);
    		if(user.isChildren() && StringUtils.isNotBlank(user.getMobile())) {
        		user = userService.getParent(user);
        		unionid = user.getUnionid();
        	}
			
			UserCourseCustomize userCourseCustomize = JsonUtils.parse(requestBody, UserCourseCustomize.class);
			userCourseCustomizeService.create(unionid, userCourseCustomize);
			
			return httpResult.success().message("定制课程信息提交成功");
			
		} catch (Exception e) {
			
			logger.error("create course customize request failed with error", e);
            return httpResult.fail("500").message("获取课程信息失败");
			
		}
	}
}
